package day11.demo;

/**
 * @Author juzipi
 * @Date 2021/6/28 10:10
 * @Info
 */
public class CheckAccount extends Account {

    private double overdraft;//可透支限额

    public CheckAccount(int id, double balance) {
        super(id, balance);
        this.overdraft = this.getOver();
    }

    private double getOver(){
        return super.getBalance() * 3;
    }

    @Override
    public void withdraw(double amount) {
        System.out.println("准备取"+amount+"元");
        if (getBalance() >= amount){
//            setBalance(getBalance() - amount);
            //调用父类的方法实现减钱的效果
            super.withdraw(amount);
            System.out.println("当前余额:"+getBalance());
        }
        //可透支金额大于等于 要取得钱减去余额 就能透支取钱
        else if (overdraft >= amount - getBalance()){
            //要取得钱减去余额得到 要透支得金额
            double overdraft = amount - getBalance();
            this.overdraft -= overdraft;
            //把余额变0
            setBalance(0);
            System.out.println("取钱成功");
            System.out.println("透支了: "+overdraft);
            System.out.println("剩余可透支金额："+this.overdraft);
        }
        else if (overdraft < amount - getBalance()){
            System.out.println("透支超过上限,取钱失败");
            System.out.println("当前余额"+getBalance());
            System.out.println("当前可透支金额"+overdraft);
        }
    }
}
